//
//  FeedbackViewController.swift
//  Exchange
//
//  Created by rui on 2018/7/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class FeedbackViewController: BaseViewController {

    
    lazy var lbl: UILabel = {
        let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
        lbl.font = UIFont.systemFont(ofSize: 15)
        lbl.text = kText("请输入反馈内容")
        lbl.themeTextColor = ThemeManager.ThemeColorKeys.text3
        return lbl
    }()
    
    lazy var viewModel: FeedbackViewModel = {
        let vm = FeedbackViewModel()
        vm.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_feedback {
                if isSuccess {
                    self?.cancel()
                    CYMBShow.showMessage("提交成功")
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        return vm
    }()
    
    lazy var textView: UITextView = {
        let text = UITextView(frame: CGRect(x: 16, y: 0, width: self.view.width - 32, height: 400))
        text.font = UIFont.systemFont(ofSize: 15)
        text.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        text.delegate = self
        text.themeTextColor = ThemeManager.ThemeColorKeys.text1
        text.themeBackground = ThemeManager.ThemeColorKeys.background
        return text
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    func initUI() {
        self.automaticallyAdjustsScrollViewInsets = false
        self.view.addSubview(textView)
        textView.addSubview(lbl)
        self.lbl.isHidden = textView.text.count > 0
        self.title = kText("用户反馈")
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: kText("取消"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancel))
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: kText("提交"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(sure))
    }
    
    @objc func cancel() {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func sure(){
        if textView.text.count <= 0 {
            CYMBShow.showMessage(kText("提交内容不能为空"))
            return
        }
        self.viewModel.uploadFeedback(content: textView.text)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
}

extension FeedbackViewController: UITextViewDelegate {

    func textViewDidChange(_ textView: UITextView) {
        self.lbl.isHidden = textView.text.count > 0
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        self.lbl.isHidden = textView.text.count > 0
        return true
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
}
